###This produces the tabular results for party reputations of traits in Replication Data for: Disputed Ownership: Parties, Issues, and Traits in the Minds of Voters by Stephen N. Goggin and Alexander G. Theodoridis

library(foreign)
library(car)
library(boot)
library(survey)

setwd("~/Desktop/DisputedOwnership_Replication")

cces <- read.csv("issue_reputation.csv")

attach(cces)

#pid3lean <- recode(pid3lean, "-1='Dem';0='Ind';1='Rep'")

##Creating Issue Variables
D_1 <- AGT_PartyIssue_Dem1 - 4
D_2 <- AGT_PartyIssue_Dem2 - 4
D_3 <- AGT_PartyIssue_Dem3 - 4
D_4 <- AGT_PartyIssue_Dem4 - 4
D_5 <- AGT_PartyIssue_Dem5 - 4
D_6 <- AGT_PartyIssue_Dem6 - 4
D_7 <- AGT_PartyIssue_Dem7 - 4
D_8 <- AGT_PartyIssue_Dem8 - 4
D_9 <- AGT_PartyIssue_Dem9 - 4
D_10 <- AGT_PartyIssue_Dem10 - 4
D_11 <- AGT_PartyIssue_Dem11 - 4
D_12 <- AGT_PartyIssue_Dem12 - 4
D_13 <- AGT_PartyIssue_Dem13 - 4
D_14 <- AGT_PartyIssue_Dem14 - 4
D_15 <- AGT_PartyIssue_Dem15 - 4

R_1 <- AGT_PartyIssue_Rep1 - 4
R_2 <- AGT_PartyIssue_Rep2 - 4
R_3 <- AGT_PartyIssue_Rep3 - 4
R_4 <- AGT_PartyIssue_Rep4 - 4
R_5 <- AGT_PartyIssue_Rep5 - 4
R_6 <- AGT_PartyIssue_Rep6 - 4
R_7 <- AGT_PartyIssue_Rep7 - 4
R_8 <- AGT_PartyIssue_Rep8 - 4
R_9 <- AGT_PartyIssue_Rep9 - 4
R_10 <- AGT_PartyIssue_Rep10 - 4
R_11 <- AGT_PartyIssue_Rep11 - 4
R_12 <- AGT_PartyIssue_Rep12 - 4
R_13 <- AGT_PartyIssue_Rep13 - 4
R_14 <- AGT_PartyIssue_Rep14 - 4
R_15 <- AGT_PartyIssue_Rep15 - 4

####################
#Create Data Frame with Essential Variables
cces_ready <- data.frame(V101,pid3lean,D_1,D_2,D_3,D_4,D_5,D_6,D_7,D_8,D_9,D_10,D_11,D_12,D_13,D_14,D_15,R_1,R_2,R_3,R_4,R_5,R_6,R_7,R_8,R_9,R_10,R_11,R_12,R_13,R_14,R_15)

#Creating Vectors for Output
MDiff_1 <- NULL
MDiff_2 <- NULL
MDiff_3 <- NULL
MDiff_4 <- NULL
MDiff_5 <- NULL
MDiff_6 <- NULL
MDiff_7 <- NULL
MDiff_8 <- NULL
MDiff_9 <- NULL
MDiff_10 <- NULL
MDiff_11 <- NULL
MDiff_12 <- NULL
MDiff_13 <- NULL
MDiff_14 <- NULL
MDiff_15 <- NULL

####################
#Bootstrapping it all

for (i in 1:10000){
	
#With weights
tempdata <- sample(1:nrow(cces_ready),nrow(cces_ready),replace=T,prob=cces_ready$weight)

partisans <- cces_ready[tempdata,]

Dem <- subset(partisans, partisans$pid3lean==-1)
Rep <- subset(partisans, partisans$pid3lean==1)	

D_D_1 <- mean(Dem$D_1, na.rm=T)
D_R_1 <- mean(Dem$R_1, na.rm=T)
R_R_1 <- mean(Rep$R_1, na.rm=T)
R_D_1 <- mean(Rep$D_1, na.rm=T)

D_D_2 <- mean(Dem$D_2, na.rm=T)
D_R_2 <- mean(Dem$R_2, na.rm=T)
R_R_2 <- mean(Rep$R_2, na.rm=T)
R_D_2 <- mean(Rep$D_2, na.rm=T)

D_D_3 <- mean(Dem$D_3, na.rm=T)
D_R_3 <- mean(Dem$R_3, na.rm=T)
R_R_3 <- mean(Rep$R_3, na.rm=T)
R_D_3 <- mean(Rep$D_3, na.rm=T)

D_D_4 <- mean(Dem$D_4, na.rm=T)
D_R_4 <- mean(Dem$R_4, na.rm=T)
R_R_4 <- mean(Rep$R_4, na.rm=T)
R_D_4 <- mean(Rep$D_4, na.rm=T)

D_D_5 <- mean(Dem$D_5, na.rm=T)
D_R_5 <- mean(Dem$R_5, na.rm=T)
R_R_5 <- mean(Rep$R_5, na.rm=T)
R_D_5 <- mean(Rep$D_5, na.rm=T)

D_D_6 <- mean(Dem$D_6, na.rm=T)
D_R_6 <- mean(Dem$R_6, na.rm=T)
R_R_6 <- mean(Rep$R_6, na.rm=T)
R_D_6 <- mean(Rep$D_6, na.rm=T)

D_D_7 <- mean(Dem$D_7, na.rm=T)
D_R_7 <- mean(Dem$R_7, na.rm=T)
R_R_7 <- mean(Rep$R_7, na.rm=T)
R_D_7 <- mean(Rep$D_7, na.rm=T)

D_D_8 <- mean(Dem$D_8, na.rm=T)
D_R_8 <- mean(Dem$R_8, na.rm=T)
R_R_8 <- mean(Rep$R_8, na.rm=T)
R_D_8 <- mean(Rep$D_8, na.rm=T)

D_D_9 <- mean(Dem$D_9, na.rm=T)
D_R_9 <- mean(Dem$R_9, na.rm=T)
R_R_9 <- mean(Rep$R_9, na.rm=T)
R_D_9 <- mean(Rep$D_9, na.rm=T)

D_D_10 <- mean(Dem$D_10, na.rm=T)
D_R_10 <- mean(Dem$R_10, na.rm=T)
R_R_10 <- mean(Rep$R_10, na.rm=T)
R_D_10 <- mean(Rep$D_10, na.rm=T)

D_D_11 <- mean(Dem$D_11, na.rm=T)
D_R_11 <- mean(Dem$R_11, na.rm=T)
R_R_11 <- mean(Rep$R_11, na.rm=T)
R_D_11 <- mean(Rep$D_11, na.rm=T)

D_D_12 <- mean(Dem$D_12, na.rm=T)
D_R_12 <- mean(Dem$R_12, na.rm=T)
R_R_12 <- mean(Rep$R_12, na.rm=T)
R_D_12 <- mean(Rep$D_12, na.rm=T)

D_D_13 <- mean(Dem$D_13, na.rm=T)
D_R_13 <- mean(Dem$R_13, na.rm=T)
R_R_13 <- mean(Rep$R_13, na.rm=T)
R_D_13 <- mean(Rep$D_13, na.rm=T)

D_D_14 <- mean(Dem$D_14, na.rm=T)
D_R_14 <- mean(Dem$R_14, na.rm=T)
R_R_14 <- mean(Rep$R_14, na.rm=T)
R_D_14 <- mean(Rep$D_14, na.rm=T)

D_D_15 <- mean(Dem$D_15, na.rm=T)
D_R_15 <- mean(Dem$R_15, na.rm=T)
R_R_15 <- mean(Rep$R_15, na.rm=T)
R_D_15 <- mean(Rep$D_15, na.rm=T)

D_1 <- D_D_1 - D_R_1
R_1 <- R_R_1 - R_D_1

D_2 <- D_D_2 - D_R_2
R_2 <- R_R_2 - R_D_2

D_3 <- D_D_3 - D_R_3
R_3 <- R_R_3 - R_D_3

D_4 <- D_D_4 - D_R_4
R_4 <- R_R_4 - R_D_4

D_5 <- D_D_5 - D_R_5
R_5 <- R_R_5 - R_D_5

D_6 <- D_D_6 - D_R_6
R_6 <- R_R_6 - R_D_6

D_7 <- D_D_7 - D_R_7
R_7 <- R_R_7 - R_D_7

D_8 <- D_D_8 - D_R_8
R_8 <- R_R_8 - R_D_8

D_9 <- D_D_9 - D_R_9
R_9 <- R_R_9 - R_D_9

D_10 <- D_D_10 - D_R_10
R_10 <- R_R_10 - R_D_10

D_11 <- D_D_11 - D_R_11
R_11 <- R_R_11 - R_D_11

D_12 <- D_D_12 - D_R_12
R_12 <- R_R_12 - R_D_12

D_13 <- D_D_13 - D_R_13
R_13 <- R_R_13 - R_D_13

D_14 <- D_D_14 - D_R_14
R_14 <- R_R_14 - R_D_14

D_15 <- D_D_15 - D_R_15
R_15 <- R_R_15 - R_D_15

Diff_1 <- D_1 - R_1
Diff_2 <- D_2 - R_2
Diff_3 <- D_3 - R_3
Diff_4 <- D_4 - R_4
Diff_5 <- D_5 - R_5
Diff_6 <- D_6 - R_6
Diff_7 <- D_7 - R_7
Diff_8 <- D_8 - R_8
Diff_9 <- D_9 - R_9
Diff_10 <- D_10 - R_10
Diff_11 <- D_11 - R_11
Diff_12 <- D_12 - R_12
Diff_13 <- D_13 - R_13
Diff_14 <- D_14 - R_14
Diff_15 <- D_15 - R_15

Diffs <- c(Diff_1,Diff_2,Diff_3,Diff_4,Diff_5,Diff_6,Diff_7,Diff_8,Diff_9,Diff_10,Diff_11,Diff_12,Diff_13,Diff_14,Diff_15)

Mean_Diff <- mean(Diffs)

MDiff_1 <- c(MDiff_1, Diff_1 - Mean_Diff)
MDiff_2 <- c(MDiff_2, Diff_2 - Mean_Diff)
MDiff_3 <- c(MDiff_3, Diff_3 - Mean_Diff)
MDiff_4 <- c(MDiff_4, Diff_4 - Mean_Diff)
MDiff_5 <- c(MDiff_5, Diff_5 - Mean_Diff)
MDiff_6 <- c(MDiff_6, Diff_6 - Mean_Diff)
MDiff_7 <- c(MDiff_7, Diff_7 - Mean_Diff)
MDiff_8 <- c(MDiff_8, Diff_8 - Mean_Diff)
MDiff_9 <- c(MDiff_9, Diff_9 - Mean_Diff)
MDiff_10 <- c(MDiff_10, Diff_10 - Mean_Diff)
MDiff_11 <- c(MDiff_11, Diff_11 - Mean_Diff)
MDiff_12 <- c(MDiff_12, Diff_12 - Mean_Diff)
MDiff_13 <- c(MDiff_13, Diff_13 - Mean_Diff)
MDiff_14 <- c(MDiff_14, Diff_14 - Mean_Diff)
MDiff_15 <- c(MDiff_15, Diff_15 - Mean_Diff)

		
}
	

####################
#Calculating the Actual Differences in Sample (Unweighted)

Dem <- subset(cces_ready, cces_ready$pid3lean==-1)
Rep <- subset(cces_ready, cces_ready$pid3lean==1)	

D_D_1 <- mean(Dem$D_1, na.rm=T)
D_R_1 <- mean(Dem$R_1, na.rm=T)
R_R_1 <- mean(Rep$R_1, na.rm=T)
R_D_1 <- mean(Rep$D_1, na.rm=T)

D_D_2 <- mean(Dem$D_2, na.rm=T)
D_R_2 <- mean(Dem$R_2, na.rm=T)
R_R_2 <- mean(Rep$R_2, na.rm=T)
R_D_2 <- mean(Rep$D_2, na.rm=T)

D_D_3 <- mean(Dem$D_3, na.rm=T)
D_R_3 <- mean(Dem$R_3, na.rm=T)
R_R_3 <- mean(Rep$R_3, na.rm=T)
R_D_3 <- mean(Rep$D_3, na.rm=T)

D_D_4 <- mean(Dem$D_4, na.rm=T)
D_R_4 <- mean(Dem$R_4, na.rm=T)
R_R_4 <- mean(Rep$R_4, na.rm=T)
R_D_4 <- mean(Rep$D_4, na.rm=T)

D_D_5 <- mean(Dem$D_5, na.rm=T)
D_R_5 <- mean(Dem$R_5, na.rm=T)
R_R_5 <- mean(Rep$R_5, na.rm=T)
R_D_5 <- mean(Rep$D_5, na.rm=T)

D_D_6 <- mean(Dem$D_6, na.rm=T)
D_R_6 <- mean(Dem$R_6, na.rm=T)
R_R_6 <- mean(Rep$R_6, na.rm=T)
R_D_6 <- mean(Rep$D_6, na.rm=T)

D_D_7 <- mean(Dem$D_7, na.rm=T)
D_R_7 <- mean(Dem$R_7, na.rm=T)
R_R_7 <- mean(Rep$R_7, na.rm=T)
R_D_7 <- mean(Rep$D_7, na.rm=T)

D_D_8 <- mean(Dem$D_8, na.rm=T)
D_R_8 <- mean(Dem$R_8, na.rm=T)
R_R_8 <- mean(Rep$R_8, na.rm=T)
R_D_8 <- mean(Rep$D_8, na.rm=T)

D_D_9 <- mean(Dem$D_9, na.rm=T)
D_R_9 <- mean(Dem$R_9, na.rm=T)
R_R_9 <- mean(Rep$R_9, na.rm=T)
R_D_9 <- mean(Rep$D_9, na.rm=T)

D_D_10 <- mean(Dem$D_10, na.rm=T)
D_R_10 <- mean(Dem$R_10, na.rm=T)
R_R_10 <- mean(Rep$R_10, na.rm=T)
R_D_10 <- mean(Rep$D_10, na.rm=T)

D_D_11 <- mean(Dem$D_11, na.rm=T)
D_R_11 <- mean(Dem$R_11, na.rm=T)
R_R_11 <- mean(Rep$R_11, na.rm=T)
R_D_11 <- mean(Rep$D_11, na.rm=T)

D_D_12 <- mean(Dem$D_12, na.rm=T)
D_R_12 <- mean(Dem$R_12, na.rm=T)
R_R_12 <- mean(Rep$R_12, na.rm=T)
R_D_12 <- mean(Rep$D_12, na.rm=T)

D_D_13 <- mean(Dem$D_13, na.rm=T)
D_R_13 <- mean(Dem$R_13, na.rm=T)
R_R_13 <- mean(Rep$R_13, na.rm=T)
R_D_13 <- mean(Rep$D_13, na.rm=T)

D_D_14 <- mean(Dem$D_14, na.rm=T)
D_R_14 <- mean(Dem$R_14, na.rm=T)
R_R_14 <- mean(Rep$R_14, na.rm=T)
R_D_14 <- mean(Rep$D_14, na.rm=T)

D_D_15 <- mean(Dem$D_15, na.rm=T)
D_R_15 <- mean(Dem$R_15, na.rm=T)
R_R_15 <- mean(Rep$R_15, na.rm=T)
R_D_15 <- mean(Rep$D_15, na.rm=T)

D_1 <- D_D_1 - D_R_1
R_1 <- R_R_1 - R_D_1

D_2 <- D_D_2 - D_R_2
R_2 <- R_R_2 - R_D_2

D_3 <- D_D_3 - D_R_3
R_3 <- R_R_3 - R_D_3

D_4 <- D_D_4 - D_R_4
R_4 <- R_R_4 - R_D_4

D_5 <- D_D_5 - D_R_5
R_5 <- R_R_5 - R_D_5

D_6 <- D_D_6 - D_R_6
R_6 <- R_R_6 - R_D_6

D_7 <- D_D_7 - D_R_7
R_7 <- R_R_7 - R_D_7

D_8 <- D_D_8 - D_R_8
R_8 <- R_R_8 - R_D_8

D_9 <- D_D_9 - D_R_9
R_9 <- R_R_9 - R_D_9

D_10 <- D_D_10 - D_R_10
R_10 <- R_R_10 - R_D_10

D_11 <- D_D_11 - D_R_11
R_11 <- R_R_11 - R_D_11

D_12 <- D_D_12 - D_R_12
R_12 <- R_R_12 - R_D_12

D_13 <- D_D_13 - D_R_13
R_13 <- R_R_13 - R_D_13

D_14 <- D_D_14 - D_R_14
R_14 <- R_R_14 - R_D_14

D_15 <- D_D_15 - D_R_15
R_15 <- R_R_15 - R_D_15

Diff_1 <- D_1 - R_1
Diff_2 <- D_2 - R_2
Diff_3 <- D_3 - R_3
Diff_4 <- D_4 - R_4
Diff_5 <- D_5 - R_5
Diff_6 <- D_6 - R_6
Diff_7 <- D_7 - R_7
Diff_8 <- D_8 - R_8
Diff_9 <- D_9 - R_9
Diff_10 <- D_10 - R_10
Diff_11 <- D_11 - R_11
Diff_12 <- D_12 - R_12
Diff_13 <- D_13 - R_13
Diff_14 <- D_14 - R_14
Diff_15 <- D_15 - R_15

Diffs <- c(Diff_1,Diff_2,Diff_3,Diff_4,Diff_5,Diff_6,Diff_7,Diff_8,Diff_9,Diff_10,Diff_11,Diff_12,Diff_13,Diff_14,Diff_15)

Mean_Diff <- mean(Diffs)

ADiff_1 <- Diff_1 - Mean_Diff
ADiff_2 <- Diff_2 - Mean_Diff
ADiff_3 <- Diff_3 - Mean_Diff
ADiff_4 <- Diff_4 - Mean_Diff
ADiff_5 <- Diff_5 - Mean_Diff
ADiff_6 <- Diff_6 - Mean_Diff
ADiff_7 <- Diff_7 - Mean_Diff
ADiff_8 <- Diff_8 - Mean_Diff
ADiff_9 <- Diff_9 - Mean_Diff
ADiff_10 <- Diff_10 - Mean_Diff
ADiff_11 <- Diff_11 - Mean_Diff
ADiff_12 <- Diff_12 - Mean_Diff
ADiff_13 <- Diff_13 - Mean_Diff
ADiff_14 <- Diff_14 - Mean_Diff
ADiff_15 <- Diff_15 - Mean_Diff

####################
#Comparing the Actual Differences and Bootstrapped Bounds

issue <- c("Education","Social Security","Poverty","Healthcare","Terrorism","Environment","Energy","National Defense","Taxes","The Economy","Crime","Budget Deficit","Jobs","Foreign Affairs","Immigration")

CI95_lower <- c(quantile(MDiff_1,.025),quantile(MDiff_2,.025)
,quantile(MDiff_3,.025),quantile(MDiff_4,.025),quantile(MDiff_5,.025),quantile(MDiff_6,.025),quantile(MDiff_7,.025),quantile(MDiff_8,.025),quantile(MDiff_9,.025),quantile(MDiff_10,.025),quantile(MDiff_11,.025),quantile(MDiff_12,.025),quantile(MDiff_13,.025),quantile(MDiff_14,.025),quantile(MDiff_15,.025))

CI95_upper <- c(quantile(MDiff_1,.975),quantile(MDiff_2,.975)
,quantile(MDiff_3,.975),quantile(MDiff_4,.975),quantile(MDiff_5,.975),quantile(MDiff_6,.975),quantile(MDiff_7,.975),quantile(MDiff_8,.975),quantile(MDiff_9,.975),quantile(MDiff_10,.975),quantile(MDiff_11,.975),quantile(MDiff_12,.975),quantile(MDiff_13,.975),quantile(MDiff_14,.975),quantile(MDiff_15,.975))

ownership_estimate_weighted <- c(ADiff_1,ADiff_2,ADiff_3,ADiff_4,ADiff_5,ADiff_6,ADiff_7,ADiff_8,ADiff_9,ADiff_10,ADiff_11,ADiff_12,ADiff_13,ADiff_14,ADiff_15)

D_ratingdiff <- c(D_1, D_2, D_3, D_4, D_5, D_6, D_7,D_8,D_9,D_10,D_11,D_12,D_13,D_14,D_15)

R_ratingdiff <- c(R_1, R_2, R_3, R_4, R_5, R_6, R_7,R_8,R_9,R_10,R_11,R_12,R_13,R_14,R_15)

raw_Diff <- c(Diff_1, Diff_2, Diff_3, Diff_4, Diff_5, Diff_6, Diff_7,Diff_8,Diff_9,Diff_10,Diff_11,Diff_12,Diff_13,Diff_14,Diff_15)

inductive_ownership <- data.frame(issue,ownership_estimate_weighted, CI95_lower,CI95_upper,raw_Diff,D_ratingdiff,R_ratingdiff)

write.csv(inductive_ownership,"IssueOwnership_PartyReputation.csv")


